package org.tnc.budget.webapp.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.regex.Pattern;



public class DateUtils {
	
	private static Pattern p = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|(1[0-9])|(2[0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
	
    public static ArrayList<String> getAllDay(Date date) {

        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        // 设置日期
        cal.setTime(date);

        // 当前月的第一天
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date beginTime = cal.getTime();
        // String beginTime1 = datef.format(beginTime);

        // 获取当月天数
        int j = cal.getActualMaximum(Calendar.DATE);

        // 将当月第一天set
        cal.setTime(beginTime);

        ArrayList<String> list = new ArrayList<String>();
        for (int i = 1; i <= j; i++) {
            cal.set(Calendar.DATE, i);
            list.add(sdf.format(cal.getTime()));
            // System.out.println(sdf.format(cal.getTime()));
        }
        return list;
    }

    public static Date getStartDateByMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        // 设置日期
        cal.setTime(date);

        // 当前月的第一天
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date beginTime = cal.getTime();

        return beginTime;
    }

    public static Date getEndDateByMonth(Date date) {

        Calendar cal = Calendar.getInstance();
        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        // 设置日期
        cal.setTime(date);

        // 当前月的最后一天
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.roll(Calendar.DAY_OF_MONTH, -1);
        Date endTime = cal.getTime();

        return endTime;

    }

    public static Date convertDate(String adateStrteStr) {
        java.util.Date date = null;
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            date = simpleDateFormat.parse(adateStrteStr);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return date;
    }
    /**
     * 起始时间与结束时间间隔天数，每天以00:00:00为开始
     * @param startDateTime
     * @param endDateTime
     * @return
     */
    public static Queue<Date> splitDate(String startDateTime, String endDateTime){
    	
    	boolean start = p.matcher(startDateTime).matches();
    	if(!start)
    		throw new IllegalArgumentException("Argument startDateTime error:"+startDateTime);
    	
    	boolean end = p.matcher(endDateTime).matches();
    	if(!end)
    		throw new IllegalArgumentException("Argument startDateTime error:"+endDateTime);
    	
    	Queue<Date> q = new LinkedBlockingDeque<Date>();
    	
    	Date startDate = convertDateHMS(startDateTime);
    	q.add(startDate);
    	
    	Date endDate = convertDateHMS(endDateTime);
    	
    	int intervalDay = getIntervalDays(startDate, endDate);
    	if(intervalDay >= 1){
    		// 
    		Calendar cal = new GregorianCalendar();
    		cal.setTime(startDate);
    		
    		for(int i=0;i<intervalDay;i++){
    			cal.roll(Calendar.DAY_OF_MONTH, true);
    			
    			cal.set(Calendar.HOUR_OF_DAY, 0);
    			cal.set(Calendar.MINUTE, 0);
    			cal.set(Calendar.SECOND, 0);
    			
    			Date currDate = cal.getTime();
    			q.add(currDate);
    		}
    		
    	}
    	
    	q.add(endDate);
    	return q;
    }

    public static Date convertDateHMS(String adateStrteStr) {
        java.util.Date date = null;
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = simpleDateFormat.parse(adateStrteStr);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return date;
    }

    /**
     * 计算两个时间之间相隔天数
     * 
     * @param startday 开始时间
     * @param endday 结束时间
     * @return 间隔天数
     */
    public static int getIntervalDays(Date startday, Date endday) {
        // 确保startday在endday之前
        if (startday.after(endday)) {
            Date cal = startday;
            startday = endday;
            endday = cal;
        }
        // 分别得到两个时间的毫秒数
        long sl = startday.getTime();
        long el = endday.getTime();

        long ei = el - sl;
        // 根据毫秒数计算间隔天数
        return (int) (ei / (1000 * 60 * 60 * 24));
    }

    /**
     * 计算两个时间之间相隔分钟数
     * 
     * @param startday 开始时间
     * @param endday 结束时间
     * @return 间隔天数
     */
    public static float getIntervalHours(Date startday, Date endday) {
        // 确保startday在endday之前
        if (startday.after(endday)) {
            Date cal = startday;
            startday = endday;
            endday = cal;
        }
        // 分别得到两个时间的毫秒数
        long sl = startday.getTime();
        long el = endday.getTime();
        
        System.out.println(startday);
        System.out.println(endday);
        System.out.println(sl);
        System.out.println(el);

        long ei = el - sl;
        // 根据毫秒数计算间隔分钟数
//        float eif = Float.valueOf(String.valueOf(ei));
//        return (float) (eif / (1000 * 60 * 60));
        return (int) (ei / (1000 * 60 * 60));
    }

    public static void main(String[] args) {

    	Queue<Date> qd = splitDate("2011-04-16 13:00:00", "2011-04-17 00:00:00");
    	for (Date date : qd) {
			
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			System.out.println(simpleDateFormat.format(date));
		}
    	System.out.println("-----------------------------");
    	
//    	Date dd = convertDateHMS("2011-4-16 13:00:00");
//    	SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		System.out.println(simpleDateFormat.format(dd));
		
		System.out.println("-----------------------------");
		
		String dayPtn = "^\\d{4}(\\-|\\/|\\.)\\d{1,2}\\1\\d{1,2}$";
		String startDay = "2011-1-10";
		boolean m = startDay.matches(dayPtn);
		if(m){
			System.out.println("Yes:"+startDay);
		} else {
			System.out.println("No:"+startDay);
		}
		
		startDay = "2011-1 10";
		boolean m2 = startDay.matches(dayPtn);
		if(m){
			System.out.println("Yes:"+startDay);
		} else {
			System.out.println("No:"+startDay);
		}
		
		String startTime = "2011-04-03 14:30:00";
		if(p.matcher(startTime).matches())
			System.out.append("asssssssssssssss");
		
		System.out.println("-----------------------------");
    }



}
